<?php
    require_once("config.php");

    Functions::PrintHeader("Messages");
    ErrorMessage::Show("Disabled","This module is disabled.  You should contact landlords via the information contained in their profile.");
    Functions::PrintFooter();
    exit;

    
    $menu = new Menu("FAQ Administration");
    $menu->AddItem(new MenuItem("Back to FAQ","faq.php",AuthConstants::ADMINISTRATORS));
    MenuManager::AddMenu("Read Messages",$menu);
    
    if ($_POST) {
        echo "<pre>" . print_r($_POST,true) . "</pre>";
        Conversation::SendMessage($_POST['recipient'], $_POST['listingid'], $_POST['message']);
        header("Location: messages.php?listingid=$_POST[listingid]&recipient=$_POST[recipient]");
    }
    else if (isset($_GET['listingid'])) {
        Functions::PrintHeader("Read Messages");
        ?>
        <form method="POST">
            <?php
                $recipient = NULL;
                $listing = ListingManager::FetchListing($_GET['listingid']);
                $recipient = (isset($_GET['recipient']) ? $_GET['recipient'] : $recipient = $listing->GetLandlord(true));
                
                echo "<input type='hidden' name='recipient' value='$recipient' />";
                echo "<input type='hidden' name='listingid' value='$_GET[listingid]' />";
                $msgs = Conversation::GetConversationMessages($_GET['listingid'],$recipient);
                
                $row = new ListingRow($_GET['listingid']);
                $row->Render();
            ?>
            <br />
            <table class='wide cgltable'>
                <tr>
                    <th colspan='2'>Conversation</th>
                </tr>
                <?php
                    if (count($msgs) > 0) {
                        foreach ($msgs as $message) {   
                            // mark message as read.
                            if ($message['recipient'] == AuthManager::GetUserID())
                                DBM::Execute("UPDATE conversations SET attributes=" . ($message['attributes'] & 1 == 1 ? 2 : $message['attributes']) . " WHERE id=$message[id];"); 
                                
                        ?>
                        <tr<?=($message['sender'] == AuthManager::GetUserID() ? " style='background:#FFFFBB;'" : " style='background:#BBFFBB;'") ?>>
                            <td>
                                <?php
                                    $senderinfo = DBM::FetchRow("SELECT * FROM guests WHERE id=$message[sender]");
                                    echo "From $senderinfo[name]<br />";
                                    echo "<span style='font-size:8pt;'>" . date("F d, Y g:i A",$message['date']) . "</span>";
                                ?>
                            </td>
                            <td><?=$message['message']?></td>
                        </tr>
                        <?php
                        }
                    ?>
                    <tr>
                        <td colspan='2' style='background:#bbbbbb;'>&nbsp;</td>
                    </tr>
                <?php } ?>
                <tr>
                    <td class='label' style='width:200px;'>To:</td>
                    <td><?php 
                        if (isset($recipient)) {    
                            $query = "SELECT * FROM guests WHERE id=$recipient;";
                            $row = DBM::FetchRow($query);
                            echo $row['name'];
                        }
                        else {
                            $landlord = $listing->GetLandlord();
                            echo $landlord['name'];
                        }
                    ?></td>
                </tr>
                <tr>
                    <td class='label'>Regarding Listing:</td>
                    <td><?php echo $listing->GetAddress(); ?></td>
                </tr>
                <tr>
                    <td class='label'>Your Message:</td>
                    <td><textarea name='message' style='height:150px; width:95%;'></textarea></td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <input type='reset' value='Start Over' />
                        <input type='submit' value="Send Message" />
                    </td>
                </tr>
            </table>
        </form>
        <?php
        Functions::PrintFooter();
    }
    else {
        Functions::PrintHeader("Read Messages");
        $msgs = Conversation::GetMessageList();
        ?>
        <table class='wide cgltable'>
            <tr>
                <th>Listing ID</th>
                <th>Listing Address</th>
                <th>Conversation With</th>
                <th>Number of Messages</th>
                <th>&nbsp;</th>
            </tr>
            <?php
            $used = NULL;
            $lastuser = NULL;
            
            foreach ($msgs as $message) {
                $userid = $message['sender'];
                if ($userid == AuthManager::GetUserID()) $userid = $message['recipient'];
                        
                if ($message['listingid'] === $last && $userid == $lastuser) {
                    continue;
                }
                
                ?>
                <tr>
                    <td><?=$message['listingid']?></td>
                    <td><?php
                        $listing = ListingManager::FetchListing($message['listingid']);
                        echo $listing->GetAddress(true);
                    ?></td>
                    <td><?php
                        $info = DBM::FetchRow("SELECT * FROM guests WHERE id=$userid");
                        echo $info['name'];
                    ?></td>
                    <td><?php
                        echo count(Conversation::GetMyMessages(Conversation::MESSAGE_VISIBLE,$message['listingid'],$userid));
                        $unread = Conversation::CountUnreadMessages($message['listingid'], $userid);
                        if ($unread > 0) {
                            ?>
                            <a href='messages.php?action=compose&listingid=<?=$message['listingid']?>&recipient=<?=$userid?>'>
                                <span style='background-color:lime; padding:3px; font-weight:bold;'> 
                                    (<?=$unread?> new)
                                </span>
                            </a>
                            <?php
                        }
                    ?></td>
                    <td><a href='messages.php?action=compose&listingid=<?=$message['listingid']?>&recipient=<?=$userid?>'>View Conversation</a></td>
                </tr>
                <?php
                $last = $message['listingid'];
                $lastuser = $userid;
            }
            ?>
        </table>
        <?php
        Functions::PrintFooter();
    }
?>
